package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kcwl.carrier.domain.entity.RegularRoutesInfo;
import com.kcwl.carrier.domain.repository.IRegularRoutesInfoRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.RegularRoutesInfoMapper;
import com.kcwl.carrier.infrastructure.repository.po.RegularRoutesInfoPO;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.KcBeanConverter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RegularRoutesInfoRepositoryImpl extends BaseRepositoryImpl<RegularRoutesInfoMapper, RegularRoutesInfoPO> implements IRegularRoutesInfoRepository {

    @Autowired
    private RegularRoutesInfoMapper regularRoutesInfoMapper;

    @Override
    public Integer addRoute(RegularRoutesInfo regularRoutesInfo) {
        RegularRoutesInfoPO regularRoutesInfoPO = KcBeanConverter.toBean(regularRoutesInfo, RegularRoutesInfoPO.class);
        regularRoutesInfoPO.setDataSource(1);
        regularRoutesInfoPO.setCreateTime(new Date());
        return regularRoutesInfoMapper.insert(regularRoutesInfoPO);
    }

    @Override
    public Integer updateRoute(RegularRoutesInfo regularRoutesInfo) {
        return regularRoutesInfoMapper.updateById(KcBeanConverter.toBean(regularRoutesInfo, RegularRoutesInfoPO.class));
    }

    @Override
    public Integer delRoute(RegularRoutesInfo regularRoutesInfo) {
        LambdaUpdateWrapper<RegularRoutesInfoPO> updateWrapper = new UpdateWrapper<RegularRoutesInfoPO>().lambda();
        updateWrapper.eq(RegularRoutesInfoPO::getId, regularRoutesInfo.getId());
        updateWrapper.eq(RegularRoutesInfoPO::getUserId, regularRoutesInfo.getUserId());
        return regularRoutesInfoMapper.delete(updateWrapper);
    }

    @Override
    public List<RegularRoutesInfo> listRoute(RegularRoutesInfo regularRoutesInfo) {
        LambdaQueryWrapper<RegularRoutesInfoPO> queryWrapper = new QueryWrapper<RegularRoutesInfoPO>().lambda();
        queryWrapper.eq(RegularRoutesInfoPO::getUserId, regularRoutesInfo.getUserId());
        if (Objects.nonNull(regularRoutesInfo.getId())) {
            queryWrapper.eq(RegularRoutesInfoPO::getId, regularRoutesInfo.getId());
        }
        if (StringUtils.isNotBlank(regularRoutesInfo.getStartProvinceCode())) {
            queryWrapper.eq(RegularRoutesInfoPO::getStartProvinceCode, regularRoutesInfo.getStartProvinceCode());
        }
        if (StringUtils.isNotBlank(regularRoutesInfo.getStartCityCode())) {
            queryWrapper.eq(RegularRoutesInfoPO::getStartCityCode, regularRoutesInfo.getStartCityCode());
        }
        if (StringUtils.isNotBlank(regularRoutesInfo.getStartCountyCode())) {
            queryWrapper.eq(RegularRoutesInfoPO::getStartCountyCode, regularRoutesInfo.getStartCountyCode());
        }
        if (StringUtils.isNotBlank(regularRoutesInfo.getTargetProvinceCode())) {
            queryWrapper.eq(RegularRoutesInfoPO::getTargetProvinceCode, regularRoutesInfo.getTargetProvinceCode());
        }
        if (StringUtils.isNotBlank(regularRoutesInfo.getTargetCityCode())) {
            queryWrapper.eq(RegularRoutesInfoPO::getTargetCityCode, regularRoutesInfo.getTargetCityCode());
        }
        if (StringUtils.isNotBlank(regularRoutesInfo.getTargetCountyCode())) {
            queryWrapper.eq(RegularRoutesInfoPO::getTargetCountyCode, regularRoutesInfo.getTargetCountyCode());
        }
        List<RegularRoutesInfoPO> list = regularRoutesInfoMapper.selectList(queryWrapper);
        List<RegularRoutesInfo> result = new ArrayList<>();
        Optional.ofNullable(list).ifPresent(l -> result.addAll(l.stream().map(item -> KcBeanConverter.toBean(item, RegularRoutesInfo.class)).collect(Collectors.toList())));
        return result;
    }
}
